package com.hjqjl.whdemo.feature.feature0.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import com.hjqjl.whdemo.base.BaseActivity;
import com.hjqjl.whdemo.databinding.ActivityScrollviewBinding;
import com.hjqjl.whdemo.widget.MyScrollView;

public class ScrollViewActivity extends BaseActivity {
    private MyScrollView scrollView;
    private LinearLayout llSVTitle;
    private LinearLayout llSVSmallTitle;
    private int mTitleHeight;
    private int mSmallTitleHeight;

    public static void start(Context context) {
        Intent starter = new Intent(context, ScrollViewActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void initData() {
        ActivityScrollviewBinding binding = ActivityScrollviewBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
        scrollView = binding.scrollViewScrollview;
        llSVTitle = binding.llScrollviewTitle;
        llSVSmallTitle = binding.llScrollviewSmallTitle;
    }

    @Override
    protected void processLogic() {
        scrollView.setOnScrollListener(new MyScrollView.OnScrollListener() {
            @Override
            public void onScroll(int scrollY) {
                mTitleHeight = llSVTitle.getHeight();
                mSmallTitleHeight = llSVSmallTitle.getHeight();
                float cha = mTitleHeight - mSmallTitleHeight;//为正数

                //自动跳转到指定位置
                if (scrollView.isScrollStop()) {
                    scrollView.setScrollStop(false);
                    if (scrollView.getStopY() < (cha / 2)) {//手离开触摸屏的时候，当悬浮小按钮位于大按钮中线上部的时候，ScrollView滚动到大按钮的顶部
                        scrollView.smoothScrollTo(0, 0);
                        scrollY = 0;
                    } else if (scrollView.getStopY() < cha) {//手离开触摸屏的时候，当悬浮小按钮位于大按钮中线下部的时候，ScrollView滚动到大按钮的底部
                        scrollView.smoothScrollTo(0, (int) cha);
                        scrollY = (int) cha;
                    }
                }

                if (scrollY == 0) {
                    llSVSmallTitle.setVisibility(View.GONE);
                } else if (scrollY > 0) {
                    llSVSmallTitle.setVisibility(View.VISIBLE);
                }

                //设置透明度
                if (scrollY <= cha) {
                    float mAlpha = scrollY / cha;
                    if (mAlpha < 0) {
                        mAlpha = 0;
                    } else if (mAlpha > 1) {
                        mAlpha = 1;
                    }
                    llSVSmallTitle.setAlpha(mAlpha);
                } else {
                    llSVSmallTitle.setAlpha(1);
                }
            }
        });
    }
}
